home *** CD-ROM | disk | FTP | other *** search
/ CD Actual 85 / CD Temático 40 Febrero 2004.iso / DOS / testdisk / src / testdisk.c < prev    next >
Encoding:
C/C++ Source or Header  |  2004-01-09  |  9.9 KB  |  405 lines

  1. /*
  2.  
  3.     File: testdisk.c
  4.  
  5.     Copyright (C) 1998-2004 Christophe GRENIER <grenier@cgsecurity.org>
  6.   
  7.     This software is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU General Public License as published by
  9.     the Free Software Foundation; either version 2 of the License, or
  10.     (at your option) any later version.
  11.   
  12.     This program is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.   
  17.     You should have received a copy of the GNU General Public License
  18.     along with this program; if not, write to the Free Software
  19.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  20.  
  21.  */
  22.  
  23. #include <stdarg.h>
  24. #include <unistd.h>    /* geteuid */
  25. #include <string.h>
  26. #include <time.h>
  27. #include <sys/time.h>
  28. #include <ctype.h>      /* toupper, tolower */
  29. #include <locale.h>    /* setlocale */
  30. #include "types.h"
  31. #include "common.h"
  32. #include "testdisk.h"
  33. #include "lang.h"
  34. #include "intrface.h"
  35. #include "godmode.h"
  36. #include "fnctdsk.h"
  37. #include "version.h"
  38.  
  39. static FILE* init_log(const char*filename,int argc, char**argv);
  40.  
  41. static FILE *f_rapport=NULL;
  42. static int f_status=0;
  43.  
  44. void ecrit_rapport_string(const char *string,const int max_length)
  45. {
  46.   int i;
  47.   for(i=0;(string[i]!='\0')&&(i<max_length);i++)
  48.     ecrit_rapport("%c",string[i]);
  49. }
  50.  
  51. int ecrit_rapport(const char *_format, ...)
  52. {
  53.   int res=0;
  54.   if(f_rapport!=NULL)
  55.   {
  56.     va_list ap;
  57.     va_start(ap,_format);
  58.     res=vfprintf(f_rapport,_format,ap);
  59.     va_end(ap);
  60.     if(fflush(f_rapport))
  61.     {
  62.       f_status=1;
  63.     }
  64.   }
  65.   return res;
  66. }
  67.  
  68. static FILE* init_log(const char*filename,int argc, char**argv)
  69. {
  70.   FILE*f_file=fopen(filename,"a");
  71.   if(f_file==NULL)
  72.     printf(msg_LOG_ERR);
  73.   else
  74.   {
  75.     int i;
  76.     time_t my_time=time(NULL);
  77.     fprintf(f_file,"\n\n%s",ctime(&my_time));
  78.     fprintf(f_file,msg_CMDLINE);
  79.     for(i=1;i<argc;i++)
  80.       fprintf(f_file," %s", argv[i]);
  81.     fprintf(f_file,"\n");
  82.   }
  83.   return f_file;
  84. }
  85.  
  86.  
  87. int main( int argc, char **argv )
  88. {
  89.   int i;
  90.   unsigned int x;
  91.   int help=0, create_log=0,paranoid=0,debug=0, dump_ind=0;
  92.   int fast_mode=0;
  93.   int align=1;
  94.   int do_analyse=1;
  95.   int do_list=0;
  96.   int test_recovery=0;
  97.   int write_used=0;
  98.   t_list_disk *list_disk=NULL;
  99.   t_list_disk *element_disk;
  100. #ifdef TESTING
  101.   srand(1);
  102. #endif
  103.   for(i=1;i<argc;i++)
  104.   {
  105.     for (x=0; x < strlen( argv[i] ); x++)
  106.      argv[i][x] = tolower( argv[i][x] );
  107.     if((strcmp(argv[i],"/test_recovery")==0) ||(strcmp(argv[i],"-test_recovery")==0))
  108.     {
  109.       test_recovery=1;
  110.       do_list=1;
  111.       create_log=1;
  112.     }
  113.     else if((strcmp(argv[i],"/dump")==0) || (strcmp(argv[i],"-dump")==0))
  114.       dump_ind=1;
  115.     else if((strcmp(argv[i],"/log")==0) ||(strcmp(argv[i],"-log")==0))
  116.       create_log=1;
  117.     else if((strcmp(argv[i],"/debug")==0) || (strcmp(argv[i],"-debug")==0))
  118.     {
  119.       debug++;
  120.       create_log=1;
  121.     }
  122.     else if((strcmp(argv[i],"/list")==0) || (strcmp(argv[i],"-list")==0))
  123.       do_list=1;
  124.     else if((strcmp(argv[i],"/help")==0) || (strcmp(argv[i],"-help")==0) || (strcmp(argv[i],"--help")==0) ||
  125.       (strcmp(argv[i],"/h")==0) || (strcmp(argv[i],"-h")==0))
  126.       help=1;
  127.     else
  128.     {
  129.       list_disk=insert_new_disk(list_disk,file_test_availability(argv[i],debug));
  130.       if(list_disk==NULL)
  131.       {
  132.     help=1;
  133.       }
  134.     }
  135.   }
  136.   printf(msg_Copyright);
  137.   if(help!=0)
  138.   {
  139.     printf(msg_Usage);
  140.     return 0;
  141.   }
  142.   if(create_log!=0)
  143.   {
  144. /*    const char *ext2fs_version=NULL; */
  145.     f_rapport=init_log("testdisk.log",argc,argv);
  146.     ecrit_rapport(msg_Copyright);
  147. #ifdef DJGPP
  148.     ecrit_rapport("Dos version");
  149. #elif defined(BSD)
  150.     ecrit_rapport("BSD version");
  151. #elif defined(LINUX)
  152.     ecrit_rapport("Linux version");
  153. #else
  154.     ecrit_rapport("Undefined OS");
  155. #endif
  156. #ifdef COMPILE_BY
  157. #ifdef COMPILE_HOST
  158. #ifdef COMPILE_TIME
  159.     ecrit_rapport(" (%s@%s, %s)",COMPILE_BY,COMPILE_HOST,COMPILE_TIME);
  160. #endif
  161. #endif
  162. #endif
  163.     ecrit_rapport("\n");
  164. /*    ext2fs_get_library_version(ext2fs_version,NULL); */
  165. /*    ecrit_rapport("%s\n\n",ext2fs_version); */
  166. #ifdef DEBUG
  167.     ecrit_rapport("Key down:      0x%x\n", KEY_DOWN);
  168.     ecrit_rapport("Key up:        0x%x\n", KEY_UP);
  169.     ecrit_rapport("Key left:      0x%x\n", KEY_LEFT);
  170.     ecrit_rapport("Key right:     0x%x\n", KEY_RIGHT);
  171.     ecrit_rapport("Key page down: 0x%x\n", KEY_NPAGE);
  172.     ecrit_rapport("Key page up:   0x%x\n", KEY_PPAGE);
  173.     ecrit_rapport("Key enter:     0x%x\n", KEY_ENTER);
  174. #endif
  175.   }
  176.   printf("Please wait...\n");
  177.   {
  178.     const char *locale;
  179.     locale = setlocale (LC_ALL, "");
  180.     if (locale==NULL) {
  181.       locale = setlocale (LC_ALL, NULL);
  182.       ecrit_rapport("Failed to set locale, using default '%s'.\n", locale);
  183.     } else {
  184.       ecrit_rapport("Using locale '%s'.\n", locale);
  185.     }
  186.   }
  187.   list_disk=hd_parse(list_disk,debug);
  188.   if(list_disk==NULL)
  189.   {
  190.     printf("No harddisk found\n");
  191. #ifndef DJGPP
  192.     if(geteuid()!=0)
  193.     {
  194.       printf("You need to be root to use TestDisk\n");
  195.       ecrit_rapport("You need to be root to use TestDisk\n");
  196.     }
  197. #endif
  198.   }
  199. #ifdef DJGPP
  200.   for(element_disk=list_disk;element_disk;element_disk=element_disk->next)
  201.   {
  202.     printf("%s",element_disk->disk->description(element_disk->disk));
  203.   }
  204. #endif
  205.   hd_parse_bis(list_disk,0);
  206.   /* save disk parameters to rapport */
  207.   ecrit_rapport("Hard disk list\n");
  208.   for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
  209.   {
  210.     printf("%s",element_disk->disk->description(element_disk->disk));
  211.     ecrit_rapport("%s",element_disk->disk->description(element_disk->disk));
  212.   }
  213.   printf("\n");
  214.   ecrit_rapport("\n");
  215.  
  216.   if(do_list!=0)
  217.   {
  218.     for(element_disk=list_disk;element_disk!=NULL;element_disk=element_disk->next)
  219.     {
  220.       interface_list(element_disk->disk,debug,test_recovery);
  221.       if(test_recovery!=0)
  222.       {
  223.     t_list_part *list_part=search_part(element_disk->disk,paranoid,debug,0,fast_mode,0);
  224.     if(debug!=0)
  225.     {
  226.       t_list_part *element;
  227.       ecrit_rapport("search_part() results\n");
  228.       for(element=list_part;element!=NULL;element=element->next)
  229.         aff_part_rapport(element_disk->disk,element->part);
  230.     }
  231.     delete_list_part(list_part);
  232.       }
  233.     }
  234.   }
  235.   else
  236.   {
  237.     if(do_curses_testdisk(debug,paranoid,dump_ind,fast_mode,align,do_analyse,list_disk))
  238.     {
  239.       printf("TestDisk need 25 lines to work.\nPlease enlarge the terminal and restart TestDisk.\n");
  240.     }
  241.   }
  242.   for(element_disk=list_disk;element_disk!=NULL;)
  243.   {
  244.     t_list_disk *element_disk_next=element_disk->next;
  245.     write_used|=element_disk->disk->write_used;
  246.     if(element_disk->disk->clean!=NULL)
  247.       element_disk->disk->clean(element_disk->disk);
  248.     FREE(element_disk->disk);
  249.     element_disk=element_disk_next;
  250.   }
  251.   if(f_rapport!=NULL)
  252.   {
  253.     ecrit_rapport("TestDisk exited normally.\n");
  254.     if(fclose(f_rapport))
  255.     {
  256.       f_status=1;
  257.     }
  258.   }
  259.   if(f_status!=0)
  260.   {
  261.     printf("TestDisk: Log file corrupted!\n");
  262.   }
  263.   else
  264.   {
  265.     printf("TestDisk exited normally.\n");
  266.   }
  267.   if(write_used!=0)
  268.   {
  269.     printf("You have to reboot for the change to take effect.\n");
  270.   }
  271.   return 0;
  272. }
  273.  
  274. #ifdef DEBUG
  275.  
  276. #include "fat.h"
  277. static int load_dump(const char*filename, unsigned char * buffer, const int size);
  278.  
  279.  
  280. int do_test()
  281. {
  282.   FILE *file_in;
  283.   unsigned char buffer[10*SECTOR_SIZE];
  284.   unsigned int taille;
  285.   t_param_disk *disk_car=(t_param_disk)MALLOC(sizeof(*disk_car));
  286.   t_diskext *partition=partition_new(0);
  287.   initscr();
  288.   noecho();
  289.   keypad(stdscr, TRUE); /* Need it to get arrow key */
  290. #ifndef DJGPP
  291.   nonl(); /*don't use for Dos version but enter will work with it... dilema */
  292. #endif
  293.   crmode();
  294.   /*  intrflush(stdscr, FALSE); */
  295.   cbreak();
  296.  
  297.   file_in=fopen("BOOT_BIN.txt","rb");
  298.   if(!file_in)
  299.     return 1;
  300.   taille=fread(buffer,1,10*SECTOR_SIZE,file_in);
  301.   fclose(file_in);
  302.   if(memcmp(buffer,"0000 ",5)==0)
  303.   {
  304.     int pos_file;
  305.     int pos_buffer2=0;
  306.     unsigned char buffer2[3*SECTOR_SIZE];
  307.     for(pos_file=5;pos_file<taille;)
  308.     {
  309.       char string[3];
  310.       if(buffer[pos_file]==' ')
  311.     pos_file++;
  312.       string[0]=buffer[pos_file];
  313.       string[1]=buffer[pos_file+1];
  314.       string[2]=0;
  315.       printf("%02X",strtol(string,NULL,16));
  316.       buffer2[pos_buffer2++]=strtol(string,NULL,16);
  317.       if(pos_buffer2%16==0)
  318.       {
  319.     pos_file+=18;
  320.     if(buffer[pos_file]==0xA || buffer[pos_file]==0xD)
  321.       pos_file++;
  322.     if(buffer[pos_file]==0xA || buffer[pos_file]==0xD)
  323.       pos_file++;
  324.     pos_file+=9;
  325.       }
  326.       else
  327.     pos_file+=2;
  328.     }
  329. /*   buffer2_size=pos_buffer2-1; */
  330.     memcpy(buffer,buffer2,3*SECTOR_SIZE);
  331.   }
  332.   printf("do_test\n");
  333.   aff_buffer(BUFFER_RESET,"Q");
  334.   dump_fat_info(buffer,P_16FAT);
  335.   test_FAT(disk_car,(struct fat_boot_sector *)&buffer, partition,1, 1);
  336.   aff_buffer(BUFFER_DISPLAY,"Q",stdscr);
  337.   return 0;
  338. }
  339.  
  340. static int load_dump(const char*filename, unsigned char * buffer, const int size)
  341. {
  342.   FILE *f_in;
  343.   unsigned char ligne[200];
  344.   int position=0;
  345.   int lu;
  346.   int etat=0;
  347.   unsigned char val=0;
  348.   int position_ligne=0;
  349.   memset(buffer,0,size);
  350.   if((f_in=fopen(filename,"r"))==NULL)
  351.     return 1;
  352. /* ecrit_rapport("load_dump %s ok\n",filename); */
  353.   while(!feof(f_in) && ((lu=fread(ligne,1,sizeof(ligne)-1,f_in))>0))
  354.   {
  355.     int i;
  356.     ligne[sizeof(ligne)-1]=0;
  357. /*   ecrit_rapport("ligne=%s,lu=%d\n",ligne,lu); */
  358.     for(i=0;i<lu;i++)
  359.     {
  360.       unsigned char car=ligne[i];
  361. /*     ecrit_rapport("etat=%d, car=%02X(%c), val=%02X, position_ligne=%d, position=%04X\n",etat,car,car,val,position_ligne,position); */
  362.       switch(etat)
  363.       {
  364.     case 0:
  365.       if(car==':')
  366.         etat=1;
  367.       break;
  368.     case 1:
  369.       if(car!=' ')
  370.       {
  371.         if(car>='0' && car<='9')
  372.           val=(val<<4)|(car-'0');
  373.         else
  374.           val=(val<<4)|(car-'A'+10);
  375.         etat=2;
  376.       }
  377.       break;
  378.     case 2:
  379.       if(car>='0' && car<='9')
  380.         val=(val<<4)|(car-'0');
  381.       else
  382.         val=(val<<4)|(car-'A'+10);
  383.       buffer[position]=val;
  384.       position++;
  385.       position_ligne++;
  386.       if(position_ligne<0x10)
  387.         etat=1;
  388.       else
  389.         etat=3;
  390.       break;
  391.     case 3:
  392.       if(car=='\n')
  393.       {
  394.         etat=0;
  395.         position_ligne=0;
  396.       }
  397.       break;
  398.       }    /* end switch */
  399.     } /* end for lu */
  400.   } /* end while */
  401.   fclose(f_in);
  402.   return 0;
  403. }
  404. #endif
  405.